﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace OpenPms.Migrations
{
    /// <inheritdoc />
    public partial class t301 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Orders_Reservations_TReservationId",
                table: "Orders");

            migrationBuilder.DropIndex(
                name: "IX_Reservations_DTCheckOut",
                table: "Reservations");

            migrationBuilder.DropIndex(
                name: "IX_Orders_TReservationId",
                table: "Orders");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Guests",
                table: "Guests")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.DropColumn(
                name: "TReservationId",
                table: "Orders")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "OrdersHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.RenameColumn(
                name: "IdCardType",
                table: "Guests",
                newName: "IdType")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<DateTime>(
                name: "DTCheckOut",
                table: "Guests",
                type: "datetime2",
                nullable: true)
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<bool>(
                name: "IsCheckOut",
                table: "Guests",
                type: "bit",
                nullable: false,
                defaultValue: false)
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<Guid>(
                name: "TArchiveId",
                table: "Accounts",
                type: "uniqueidentifier",
                nullable: true)
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "AccountsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Guests",
                table: "Guests",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "archiveGuests",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    IdNo = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    IdType = table.Column<int>(type: "int", nullable: false),
                    Preference = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DTCheckIn = table.Column<DateTime>(type: "datetime2", nullable: false),
                    DTCheckOut = table.Column<DateTime>(type: "datetime2", nullable: false),
                    Mobile = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    HotelId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_archiveGuests", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Archives",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ArchiveType = table.Column<int>(type: "int", nullable: false),
                    RoomTypeId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    RoomTypeName = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    Quantity = table.Column<int>(type: "int", nullable: false),
                    RateId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    RateName = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    TotalFee = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: false),
                    Prices = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    IsTeam = table.Column<bool>(type: "bit", nullable: false),
                    TeamName = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true),
                    ContactName = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true),
                    ContactMobile = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: true),
                    SouceId = table.Column<int>(type: "int", nullable: false),
                    SourceName = table.Column<string>(type: "nvarchar(16)", maxLength: 16, nullable: false),
                    OtaOrderId = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
                    Memo = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Version = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: true),
                    HotelId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Archives", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ArchiveAccounts",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ArchiveId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    AccountType = table.Column<int>(type: "int", nullable: false),
                    PayMode = table.Column<int>(type: "int", nullable: false),
                    Amount = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: false),
                    Foregift = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: false),
                    DT = table.Column<DateTime>(type: "datetime2", nullable: false),
                    Payer = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Memo = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    HotelId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ArchiveAccounts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ArchiveAccounts_Archives_ArchiveId",
                        column: x => x.ArchiveId,
                        principalTable: "Archives",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ArchiveDetails",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    RoomNo = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
                    DTCheckIn = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DTCheckOut = table.Column<DateTime>(type: "datetime2", nullable: true),
                    Memo = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ArchiveId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Guests = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    HotelId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ArchiveDetails", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ArchiveDetails_Archives_ArchiveId",
                        column: x => x.ArchiveId,
                        principalTable: "Archives",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Accounts_TArchiveId",
                table: "Accounts",
                column: "TArchiveId");

            migrationBuilder.CreateIndex(
                name: "IX_ArchiveAccounts_ArchiveId",
                table: "ArchiveAccounts",
                column: "ArchiveId");

            migrationBuilder.CreateIndex(
                name: "IX_ArchiveDetails_ArchiveId",
                table: "ArchiveDetails",
                column: "ArchiveId");

            migrationBuilder.CreateIndex(
                name: "IX_Archives_HotelId_ContactName_ContactMobile",
                table: "Archives",
                columns: new[] { "HotelId", "ContactName", "ContactMobile" });

            migrationBuilder.CreateIndex(
                name: "IX_Archives_HotelId_OtaOrderId",
                table: "Archives",
                columns: new[] { "HotelId", "OtaOrderId" });

            migrationBuilder.AddForeignKey(
                name: "FK_Accounts_Archives_TArchiveId",
                table: "Accounts",
                column: "TArchiveId",
                principalTable: "Archives",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Orders_Reservations_ReservationId",
                table: "Orders",
                column: "ReservationId",
                principalTable: "Reservations",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Accounts_Archives_TArchiveId",
                table: "Accounts");

            migrationBuilder.DropForeignKey(
                name: "FK_Orders_Reservations_ReservationId",
                table: "Orders");

            migrationBuilder.DropTable(
                name: "ArchiveAccounts");

            migrationBuilder.DropTable(
                name: "ArchiveDetails");

            migrationBuilder.DropTable(
                name: "archiveGuests");

            migrationBuilder.DropTable(
                name: "Archives");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Guests",
                table: "Guests")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.DropIndex(
                name: "IX_Accounts_TArchiveId",
                table: "Accounts");

            migrationBuilder.DropColumn(
                name: "DTCheckOut",
                table: "Guests")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.DropColumn(
                name: "IsCheckOut",
                table: "Guests")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.DropColumn(
                name: "TArchiveId",
                table: "Accounts")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "AccountsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.RenameColumn(
                name: "IdType",
                table: "Guests",
                newName: "IdCardType")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GuestsHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<Guid>(
                name: "TReservationId",
                table: "Orders",
                type: "uniqueidentifier",
                nullable: true)
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "OrdersHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", null)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Guests",
                table: "Guests",
                columns: new[] { "HotelId", "Id" });

            migrationBuilder.CreateIndex(
                name: "IX_Reservations_DTCheckOut",
                table: "Reservations",
                column: "DTCheckOut");

            migrationBuilder.CreateIndex(
                name: "IX_Orders_TReservationId",
                table: "Orders",
                column: "TReservationId");

            migrationBuilder.AddForeignKey(
                name: "FK_Orders_Reservations_TReservationId",
                table: "Orders",
                column: "TReservationId",
                principalTable: "Reservations",
                principalColumn: "Id");
        }
    }
}
